package pos.dato;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import Utiles.UIDGenerator;
import pos.dato.ConnectionManager;
import pos.dominio.Comentario;
import pos.dominio.Etiqueta;
import pos.dominio.Evento;
import pos.dominio.Usuario;


public class JDBCEventoDAO implements IEventoDAO {
    
	private Connection conn;
    IComentarioDAO cdao;
    IEtiquetaDAO edao;
    IUsuarioDAO udao;

    public JDBCEventoDAO() {
        conn = ConnectionManager.getInstance().checkOut();
        edao = new JDBCEtiquetaDAO();
        udao = new JDBCUsuarioDAO();
    }
    public boolean insert(String noticia,String fechaP,String etiquetaID,String usuarioID) {
        boolean res=false;
    	PreparedStatement stmt = null;
    	String id=UIDGenerator.getInstance().getKey();
        String sql = "INSERT INTO Eventos ( eventoID,noticia, fechaPublicacion, etiquetaID, usuarioID,voto,votado) VALUES ( ?, ?, ?, ?, ?,?,?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, id);
            stmt.setString(2, noticia);
            stmt.setString(3,fechaP);
            stmt.setString(4, etiquetaID);
            stmt.setString(5, usuarioID);
            stmt.setInt(6, 0);
            stmt.setInt(7, 0);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                    res=true;
                }
            } catch (SQLException e) {
            }
        }return res;
        }
    public void insertarComentarioEnEvento(Evento ev,Comentario comentario){
    	Connection conn = ConnectionManager.getInstance().checkOut();
    	PreparedStatement stmt = null;
    	String comID=comentario.getComentarioID();
    	String eventoID=ev.getEventoID();
        String sql = "UPDATE Comentarios SET eventoID='"+eventoID+"'"+"WHERE comentarioID='"+comID+"'";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            
            }
        }
        
    }

    public boolean asociarUConE(String usuarioID,String eventoID){
    	boolean res=false;
    	PreparedStatement stmt = null;
    	String sql = "INSERT INTO Apuntarse (usuarioID,eventoID) VALUES (?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, usuarioID);
            stmt.setString(2, eventoID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                    res=true;
                }
            } catch (SQLException e) {
            }
        }return res;
    }
    public Evento selectEvento(String eventoID) {
        PreparedStatement stmt = null;
        ResultSet result = null;
        Evento ev = new Evento();   
       
	    try {
	    	String sql = "SELECT * FROM Eventos WHERE (eventoID = ?) ";
	        stmt = conn.prepareStatement(sql);
	        stmt.setString(1, eventoID);
	        result = stmt.executeQuery();
	        result.next();
	        ev.setEventoID(result.getString("evento"));
	        ev.setNoticia(result.getString("noticia"));
	        ev.setFechaPublicacion(result.getString("fechaPublicacion"));
	        ev.setNick(result.getString("usuarioID"));
            ev.setEtiqueta(result.getString("etiquetaID"));
                      
	            
	    } catch (SQLException e) {
	        System.out.println("Message: " + e.getMessage());
	        System.out.println("SQLState: " + e.getSQLState());
	        System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	        	 ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e){} 
	        }
    
	      return ev ;
    }
  
    public List<Evento> selectAllEventos() {
        PreparedStatement stmt = null;
        ResultSet result = null;
        List<Evento> lo = new LinkedList<Evento>();
        String sql = "SELECT * FROM Eventos ORDER BY fechaPublicacion";

    try {
        stmt = conn.prepareStatement(sql);
        result = stmt.executeQuery();
        while (result.next()) {
        	Evento ev = new Evento();
            ev.setEventoID(result.getString("eventoID"));
            ev.setNoticia(result.getString("noticia"));
	        ev.setFechaPublicacion(result.getString("fechaPublicacion"));
	        ev.setVoto(result.getInt("voto"));
	        ev.setNick(result.getString("usuarioID"));
	        ev.setEtiqueta(result.getString("etiquetaID"));
           lo.add(ev);
        }
    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return lo;
    }
    
    
    public List<Evento> selectAllEventosEtiqueta(String etiquetaID) {
        PreparedStatement stmt = null;
        ResultSet result = null;
        List<Evento> lo = new LinkedList<Evento>();
        String sql = "SELECT * FROM Eventos WHERE (etiquetaID=?) ORDER BY fecha";

    try {
        stmt = conn.prepareStatement(sql);
        stmt.setString(1, etiquetaID);
        result = stmt.executeQuery();
        while (result.next()) {
        	Evento ev = new Evento();
        	ev.setEventoID(result.getString("eventoID"));
            ev.setEventoID(result.getString("evento"));
	        ev.setNoticia(result.getString("noticia"));
	        ev.setFechaPublicacion(result.getString("fechaPublicacion"));
	        ev.setVoto(result.getInt("voto"));
	        ev.setNick(result.getString("usuarioID"));
	        ev.setEtiqueta(result.getString("etiquetaID"));
            
            lo.add(ev);
        }
    } catch (SQLException e) {
        System.out.println("Message: " + e.getMessage());
        System.out.println("SQLState: " + e.getSQLState());
        System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {}
        }
        return lo;
    } 
    public void updateVoto( String eventoID, Integer a){
    	PreparedStatement stmt = null;
    	Connection conn = ConnectionManager.getInstance().checkOut();
    	try {
    		String sql ="UPDATE Eventos SET voto="+a+" WHERE eventoID='"+eventoID+"'";
    		System.out.println("La consulta en string es: "+sql);
            stmt = conn.prepareStatement(sql);
            int resultado = stmt.executeUpdate();
            System.out.println("El resultado de la consulta es:"+ resultado);
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }  
	
    
    
}
